package com.jp.knowledge.activity;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.jp.knowledge.R;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.fragment.TeamDocFragment;
import com.jp.knowledge.my.model.OrganizaModel;
import com.jp.knowledge.view.TabView;

import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/6/15.
 */

public class TeamDocActivity extends SlidingActivity {

	@ViewInject(R.id.tab_view)
	private TabView tabView;
	@ViewInject(R.id.order_viewpager)
	private ViewPager viewPager;

	private List<String> tabNames;
	private List<OrganizaModel> organizaModels;
	private List<TeamDocFragment> fragments;

	@Override
	protected int getResourceId() {
		return R.layout.activity_order;
	}

	@Override
	protected void init() {
		initTitleBar();
		tabNames = new ArrayList<>();
		organizaModels = new ArrayList<>();
		fragments = new ArrayList<>();
		viewPager.setBackgroundColor(getResources().getColor(R.color.white));
		viewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
			@Override
			public Fragment getItem(int position) {
				return fragments.get(position);
			}
			@Override
			public int getCount() {
				return fragments.size();
			}
		});
		initFragment();
		initTabView();

		String companyId = getIntent().getStringExtra("companyId");
		for(int i = 0; i < organizaModels.size(); i++){
			if(organizaModels.get(i).getCompanyId().equals(companyId)){
				viewPager.setCurrentItem(i);
				break;
			}
		}
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		String companyId = intent.getStringExtra("companyId");
		for(int i = 0; i < organizaModels.size(); i++){
			if(organizaModels.get(i).getCompanyId().equals(companyId)){
				viewPager.setCurrentItem(i);
				break;
			}
		}
	}

	private void initTitleBar(){
		topName.setText("团队文档");
		leftIcon.setImageResource(R.mipmap.arrow_white_left);
		rightIcon.setVisibility(View.GONE);
		leftIcon.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});
	}

	private void initTabView(){
		if(tabNames.size() <= 1)
			tabView.setVisibility(View.GONE);
		else
			tabView.setVisibility(View.VISIBLE);
		tabView.setupWithViewPager(viewPager);
		tabView.setTabItems(tabNames);
	}

	private void initFragment(){
		organizaModels.clear();
		organizaModels.addAll(getCompanyList());
		fragments.clear();
		tabNames.clear();
		for(OrganizaModel organizaModel : organizaModels) {
			fragments.add(TeamDocFragment.newInstance(null, organizaModel.getCompanyId()));
			tabNames.add(organizaModel.getCompanyName());
		}
		viewPager.getAdapter().notifyDataSetChanged();
	}

	private List<OrganizaModel> getCompanyList(){
		try {
			return application.getUserData().getCompanyList();
		} catch (Exception e){
			return new ArrayList<>();
		}
	}
}
